<template>

  <div class="wrapper" ref="wrapper">
    <div class="content">
      <slot/>
    </div>
  </div>

</template>

<script>
  import BScroll from '@better-scroll/core';
  import Pullup from '@better-scroll/pull-up';

  BScroll.use(Pullup);

  export default {
    name: 'Scroll',

    props: {
      probeType: {
        type: Number,
        default() {
          return 0;
        }
      },
      pullUpLoad: {
        type: Boolean,
        default() {
          return false;
        }

      }
    },

    data() {
      return {
        scroll: null
      };
    },

    mounted() {

      //因为数据是异步请求的，在没有请求到数据之前检测到不需要滚动，所以要让scroll在dom更新后执行

      this.scroll = new BScroll(this.$refs.wrapper, {
        click: true,
        probeType: this.probeType,
        pullUpLoad: this.pullUpLoad
      });

      if (this.probeType === 2 || this.probeType === 3) {
        this.scroll.on('scroll', (position) => {
          // console.log('position', position);

          this.$emit('scroll', position);
        });
      }


      if(this.pullUpLoad){
        this.scroll.on('pullingUp', () => {
          // console.log('监听滑动到底部')

          this.$emit('loadMore');
        });
      }



      //console.log(999, this.scroll);


    },

    methods: {

      scrollTo(x, y, time = 1000) {
        // if (this.scroll) {
        //   this.scroll.scrollTo(x, y, time);
        // }

        this.scroll && this.scroll.scrollTo(x, y, time);

      },

      finishLoadMore() {
        // if (this.scroll) {
        //   this.scroll.finishPullUp();
        // }
        //先判断这个scroll不为，再执行
        this.scroll && this.scroll.finishPullUp();
      },

      refresh() {
        this.scroll.refresh();
      },

      getScrollY() {
        return this.scroll ? this.scroll.y : 0;
      }


    },

    updated() {
      // this.scroll = new BScroll(this.$refs.wrapper, {
      //   click: true,
      //   probeType: this.probeType,
      //   pullUpLoad: this.pullUpLoad
      // });


      // if (this.scroll) {
      //   console.log("refresh")
      //   this.scroll.refresh();
      // }

    }
  };
</script>

<style scoped>

</style>
